<!DOCTYPE HTML>

<html>
<head>
<title>OffscreenCanvas2d with resizing: red rectangle in the middle.</title>
<style type="text/css">
.nomargin {
  margin: 0px auto;
}
</style>
<script id="myWorker" type="text/worker">
self.onmessage = function(e) {
  var transferred = e.data;
  var ctx2d = transferred.getContext('2d');
  ctx2d.fillStyle = "purple";
  ctx2d.fillRect(0, 0, transferred.width, transferred.height);
  // The placeholder canvas should initially display a 200X200 purple image and
  // then display a 40X50 green image containing a red rectangle in the middle.
  requestAnimationFrame(() => {
      // Resize offscreenCanvas from 200X200 to 40X50.
      transferred.width = 40;
      transferred.height = 50;

      ctx2d.fillStyle = "green";
      ctx2d.fillRect(0, 0, transferred.width, transferred.height);
      ctx2d.fillStyle = "red";
      ctx2d.fillRect(10, 10, 20, 20);
      requestAnimationFrame(() => {
        self.postMessage("");
      });
   });
};
</script>
<script>
var g_swapsBeforeAck = 15;

function makeWorker(script)
{
   var blob = new Blob([script]);
   return new Worker(URL.createObjectURL(blob));
}

function waitForFinish()
{
  if (g_swapsBeforeAck == 0) {
    domAutomationController.send("SUCCESS");
  } else {
    g_swapsBeforeAck--;
    document.getElementById('container').style.zIndex = g_swapsBeforeAck + 1;
    window.requestAnimationFrame(waitForFinish);
  }
}

function main()
{
  var canvas2D = document.getElementById("c");
  var offscreenCanvas = canvas2D.transferControlToOffscreen();
  var worker = makeWorker(document.getElementById("myWorker").textContent);
  worker.onmessage = function (e) {
      waitForFinish();
  };
  worker.postMessage(offscreenCanvas, [offscreenCanvas]);
}
</script>
</head>
<body onload="main()">
<div style="position:relative; width:200px; height:200px; background-color:white">
</div>
<div id="container" style="position:absolute; top:0px; left:0px">
<canvas id="c" width="200" height="200" class="nomargin"></canvas>
</div>
</body>
</html>
